<template>
    <div class="channel-edit">
        <div class="my-channels">
            <van-cell title="我的频道" :border="false" class="title-text">
                <van-button type="danger" plain round size="mini" slot="default" class="edit-btn"
                    @click="isEdit = !isEdit">
                    {{ isEdit ? '完成' : '编辑' }}
                </van-button>
            </van-cell>
            <van-grid :gutter="10">
                <van-grid-item v-for="(item, index) in myChannels" :key="item.id" class="grid-item"
                    @click="onMychannelClick(item, index)">
                    <van-icon slot="icon" name="clear" v-show="isEdit && !flexChannels.includes(item.id)"
                        class="my-channel" />
                    <span slot="text" class="grid-item-text" :class="{ active: (index === active) }">{{ item.name
                    }}</span>
                </van-grid-item>
            </van-grid>
        </div>

        <div class="channels-recommend">
            <van-cell title="频道推荐" :border="false" class="title-text" />
            <van-grid :gutter="10" direction="horizontal">
                <van-grid-item v-for="item in recommendChannels" :key="item.id" :text="item.name" class="grid-item"
                    @click="onAddChannel(item)">
                    <template #icon>
                        <van-icon name="plus" size="0.32rem" />
                    </template>
                </van-grid-item>
            </van-grid>
        </div>
    </div>
</template>

<script>
import { getAllChannels, addUserChannel, deleteUserChannel } from '../../../api/channel'
import { getUserChannels } from '../../../api/user'
import { mapState } from 'vuex'
import { setItem } from '../../../utils/storage'

export default {
    props: {
        myChannels: {
            type: Array,
            requierd: true
        },
        active: {
            type: Number,
            requierd: true
        }
    },
    data() {
        return {
            allChannels: [],
            isEdit: false,
            flexChannels: [0, 2]
        }
    },
    created() {
        this.loadAllChannels()
    },
    methods: {
        async loadAllChannels() {
            try {
                const { data } = await getAllChannels()
                this.allChannels = data.data.channels
            } catch (err) {
                console.log('请求失败', err);
            }
        },
        async onAddChannel(channel) {
            this.myChannels.push(channel)
            if (this.user) {
                await addUserChannel({
                    id: channel.id,
                    seq: this.myChannels.length
                })
                await getUserChannels()
            } else {
                setItem('toutiao_channels', this.myChannels)
            }
        },
        onMychannelClick(channel, index) {
            if (this.isEdit) {
                if (this.flexChannels.includes(channel.id)) return
                if (index <= this.active) {
                    this.$emit('updateActive', this.active - 1)
                }
                this.myChannels.splice(index, 1)
                this.deleteChannel(channel)
            } else {
                this.$emit('updateActive', index, false)
            }
        },
        async deleteChannel(channel) {
            if (this.user) {
                await deleteUserChannel(channel.id)
            } else {
                setItem('toutiao_channels', this.myChannels)
            }
        }
    },
    computed: {
        recommendChannels() {
            const channels = []
            this.allChannels.forEach(item => {
                const res = this.myChannels.find(myitem => {
                    return item.id === myitem.id
                })
                if (!res) {
                    channels.push(item)
                }
            })
            return channels
        },
        ...mapState(['user']),
    }
}
</script>

<style scoped lang="less">
.channel-edit {
    padding: 85px 0;

    .title-text {
        font-size: 32px;
        color: #333;
    }

    .grid-item {
        width: 160px;
        height: 86px;
    }

    .grid-item-text {
        font-size: 28px;
        color: #222;
    }

    :deep(.van-grid-item__content) {
        background-color: #f4f5f6;
        white-space: nowrap;

        .van-grid-item__text {
            font-size: 28px;
            color: #222;
        }
    }

    .my-channels {
        .edit-btn {
            width: 104px;
            height: 48px;
            font-size: 26px;
            color: #f85959;
            border: 1px solid #f85959;
        }

        .van-grid-item__icon+.van-grid-item__text {
            margin-top: 0;
            margin-right: 0;
        }

        :deep(.van-grid-item__icon-wrapper) {
            display: flex;
            align-items: center;
            position: absolute;
            top: -6px;
            right: -6px;
            color: #cacaca;
            font-size: 15px;
            z-index: 2;
        }

        .active {
            color: red;
        }
    }

    .channels-recommend {
        margin-top: 60px;

        :deep(.van-grid-item__icon-wrapper) {
            display: flex;
            align-items: center;
            margin-right: 6px;
        }
    }
}
</style>